package com.b2c.config;

import com.b2c.properties.SendEmailProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
public class JavaMailSendConfiguration {
    @Autowired
    private SendEmailProperties sendEmailProperties;

    @Bean
    public JavaMailSenderImpl javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setPort(sendEmailProperties.getPort());
        javaMailSender.setUsername(sendEmailProperties.getSendUserMail());
        javaMailSender.setPassword(sendEmailProperties.getAuthorizationCode());
        javaMailSender.setDefaultEncoding(sendEmailProperties.getDefaultEncoding());

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.timeout", sendEmailProperties.getTimeOut());
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

}
